/**
 * @class ElggMap
 */
ElggMap.provide( '', {
	/**
	 * @private
	 * flag used to check if user is currently editing a map
	 */
	_editing:false,
	/**
	 * Sets the map in edit mode if the user can  edit it.
	 * - Shows Edit form (the form for the map attributs).
	 * - Enables edition on the layers.
	 * 
	 * @param mapData {Object} An object that at least 
	 * has a property guid set for an existing emMap
	 * Example:
	 * 
	 * ElggMap.edit({guid: 13})
	 */
	edit : function(mapData)
	{
		if (ElggMap.requireEditAccess(mapData.guid) ) {
			ElggMap.showEditForm(mapData);
			$.each(ElggMap.layers, function(i, layer) {
				layer.enableEdition();
			});
			ElggMap.editing(true);
			ElggMap.Event.fire( "map.edit.started" );
		}
		
	},
	/**
	 */
	newMap: function()
	{
		if (!ElggMap.isLoggedIn()) {
			ElggMap.log('You must login before creating a new map');
			return;
		}
		ElggMap.unloadCurrentMap();		
		
		ElggMap.api('map.createNew', {}, function(mapData)
		{
			if (!mapData) {
				ElggMap.log("Couldn't create map");
				return;
			}
			
			ElggMap.Event.fire( "map.created" );

			ElggMap.loadEmMap(mapData.guid, function(mapData) {
				// set currentMap
				ElggMap.mapGUID = mapData.guid;

				ElggMap.edit( mapData );							
			});
			
			

		});
				
	},
	/**
	 */
	requireEditAccess: function(mapGUID)
	{
		response = ElggMap.api('map.canEdit', {guid: mapGUID});
		if (response.response) {
			return response.response;
		} else if ( mapGUID) {
			ElggMap.log('No puede editar este mapa');
		} else {
			ElggMap.log('Necesita iniciar sesión para poder crear un nuevo mapa');
		}
		return false;
		
	},
	
	/**
	 * Magically saves the map 
	 */
	stopEdit: function()
	{
		if ( ElggMap.editing()) {
			if ( ElggMap.layers[0] !== undefined ) {
				ElggMap.layers[0].disableEdition();
			}
			ElggMap.editing(false);						
			emMapData = ElggMap.saveFromForm();
			ElggMap.hideEditForm();
		}
		
		ElggMap.showMapMetadata(emMapData);
		

		ElggMap.Event.fire( "map.edit.stopped" );
	},
	/**
	 */
	getDirtyFeatures: function(justTheAttributes)
	{
		justTheAttributes =  (justTheAttributes != 'undefined')  ? true : false;
		
		var modifiedFeatures = [];
		$.each(ElggMap.layers, function(i, layer) {
			var layerModifiedFeatures = layer.getDirtyFeatures(justTheAttributes);
			$.each(layerModifiedFeatures, function( j, feature )  {
				modifiedFeatures.push(feature);
			});
			layer.clearDirtyFeatures();
		});
		return modifiedFeatures;
	},
	
	/**
	 */
	getDeletedFeatures: function(justTheAttributes)
	{
		justTheAttributes = (justTheAttributes != 'undefined') ? true : false;
		var deletedFeatures = [];
		deletedFeatures = ElggMap.deleteQueue;
		// $.each(ElggMap.layers, function(i, layer) {
			// deletedFeatures.push(layer.getDeletedFeatures(justTheAttributes) );
		// });
		return deletedFeatures;
	},
	/**
	 */
	saveFromForm: function()
	{
		var metadata = $('#emMapSaveForm').serializeObject();
		
		if ( ElggMap.requireEditAccess( metadata.mapGUID ) ) {
			var dirtyFeatures = ElggMap.getDirtyFeatures();
			
			var deletedFeatures = ElggMap.getDeletedFeatures();
			emMapData = ElggMap.save(metadata, dirtyFeatures,deletedFeatures);
			return emMapData;
		}
		return null;
	},
	/**
	 */
	save: function(metadatax, dirtyFeatures, deletedFeatures)
	{	
		if ( ElggMap.requireEditAccess(metadatax.mapGUID) ) {	
			data = {
				mapMetadata: metadatax,
				dirtyFeatures : dirtyFeatures,
				deletedFeatures : deletedFeatures
			};
			emMapData = ElggMap.api('map.save', data);
		}
		if (emMapData) {
			ElggMap.Event.fire( "map.saved" );
		}
		return emMapData;		
	},
	
	/**
	 * With parmeters, flags that the user is editing a map meaning
	 * the map will be saved when calling stopEdit
	 
	 * Called without parameters, it returns true if the user is editing a map.
	 *
	 * @param {Boolean} [setEditing] True for marking the feature as dirty.
	 * @returns {Boolean} True if the feature is marked as dirty. False otherwise.
	 */
	editing: function(setEditing)
	{
		if (setEditing !== undefined ) {
			ElggMap._editing = (setEditing) ? true : false;
		} 
		return ElggMap._editing;
	}
});
